//
//  StudioApplyBasicViewController.m
//  Arts1
//
//  Created by 孤星晨宇 on 15/10/7.
//  Copyright © 2015年 Arts1. All rights reserved.
//

#import "StudioApplyBasicViewController.h"
#import "AllHeader.h"
#import <AFNetworking.h>

// 课程报名
//#define applyURL @"http://arts1.dlab.com.cn/app/studyapply"
#define applyURL @"http://arts1.cn/app/studyapply"

// 课程报名状态
//#define ASURL @"http://arts1.dlab.com.cn/app/getapplystatus"
#define ASURL @"http://arts1.cn/app/getapplystatus"


@interface StudioApplyBasicViewController ()<UITextFieldDelegate>

@end

@implementation StudioApplyBasicViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    [self modifySubView];
    
}

- (void)modifySubView{
    _studio.text = @"北京联艺画室";
    
    // 设置代理
    _name.delegate = self;
    _phoneNum.delegate = self;
    
    _applyBTN.layer.cornerRadius = 4;
    _applyBTN.layer.masksToBounds = YES;
    [_applyBTN addTarget:self action:@selector(clickApplyBTN:) forControlEvents:(UIControlEventTouchUpInside)];
}

/**
 * 点击立即报名
 */
- (void)clickApplyBTN:(UIButton *)btn{
    if (_ID) {
        [self requestStatusForApply];
    }
    
}

// 网络请求
- (void)requestStatusForApply{
    AFHTTPRequestOperationManager *manage = [AFHTTPRequestOperationManager manager];
    manage.responseSerializer.acceptableContentTypes  =[NSSet setWithObject:@"text/html"];
    manage.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    NSDictionary *userDic = [[NSUserDefaults standardUserDefaults] objectForKey:@"dataForKeyAndUid"];
    
    //请求参数
    NSDictionary *pardic = @{@"uid":userDic[@"data"][@"uid"],@"studyId":_ID,@"key":userDic[@"data"][@"key"]};
    
    [manage POST:ASURL parameters:pardic
         success:^(AFHTTPRequestOperation *operation, id responseObject)
     {
         // 请求成功的时候调用这个block
         NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:responseObject options:(NSJSONReadingMutableContainers) error:nil];
         //返回的状态
         int index = [dic[@"status"] intValue];
         switch (index)
         {
             case 1:
             {
               // status为0时，表明已经报过名
               //信息提示框
            [MBProgressHUD show:@"已报名，不能重复报名" icon:nil view:self.view];
             }
                 break;
             case 0:
             {
                 // status为0时，表明未曾报名，需要进一步报名
                 [self requestDataForApply];
             }
                 break;
             default:
                 break;
         }
         
         NSLog(@"报名状态返回数据 ： %@", dic);
         
     } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
         // 请求失败的时候调用调用这个block
         NSLog(@"请求失败");
     }];
}

// 网络请求
- (void)requestDataForApply{
    AFHTTPRequestOperationManager *manage = [AFHTTPRequestOperationManager manager];
    manage.responseSerializer.acceptableContentTypes  =[NSSet setWithObject:@"text/html"];
    manage.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    NSDictionary *userDic = [[NSUserDefaults standardUserDefaults] objectForKey:@"dataForKeyAndUid"];
    //请求参数
    NSDictionary *pardic = @{@"spaceName":_studio.text,@"uid":userDic[@"data"][@"uid"],@"studyId":_ID,@"key":userDic[@"data"][@"key"],@"contactName": _name.text,@"contactphone":_phoneNum.text};
    
    [manage POST:applyURL parameters:pardic
         success:^(AFHTTPRequestOperation *operation, id responseObject)
     {
         // 请求成功的时候调用这个block
         NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:responseObject options:(NSJSONReadingMutableContainers) error:nil];
         //记录请求返回结果
         NSString *statusStr = nil;
         //返回的状态
         int index = [dic[@"status"] intValue];
         // 课程报名，返回结果
         switch (index)
         {
             case 1:
             {
                 statusStr = @"操作成功";
             }
                 break;
             case 0:
                 statusStr = @"参数不正确";
                 break;
             case -1:
                 statusStr = @"电话号码不符合规范";
                 break;
             case -3:
                 statusStr = @"无相关课程信息";
                 break;
             case -4:
                 statusStr = @"用户名或密码为空";
                 break;
             case -5:
                 statusStr = @"用户名或密码错误";
                 break;
             default:
                 break;
         }
         
         if (statusStr)
         {
             //信息提示框
             [MBProgressHUD show:statusStr icon:nil view:self.view];
         }
         
         NSLog(@"课程报名 -- 返回数据 ： %@", dic);
         
     } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
         // 请求失败的时候调用调用这个block
         NSLog(@"请求失败");
     }];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    if (textField == _name) {
        [_name resignFirstResponder];
        [_phoneNum becomeFirstResponder];
    } else {
        [_phoneNum resignFirstResponder];
    }
    return YES;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
